import 'package:logger/logger.dart';

class LogUtils {
  static final LogUtils shared = LogUtils._internal();
  late Logger _logger;
  bool _isInitialized = false;

  factory LogUtils() {
    return shared;
  }

  LogUtils._internal();

  Future<void> init() async {
    if (_isInitialized) {
      w('LogUtils has already been initialized!');
      return;
    }

    _logger = Logger(
      printer: PrettyPrinter(
        methodCount: 2,
        errorMethodCount: 8,
        lineLength: 120,
        colors: true,
        printEmojis: true,
        dateTimeFormat: DateTimeFormat.onlyTimeAndSinceStart,
      ),
    );

    _isInitialized = true;
    i('LogUtils initialized successfully');
  }

  void _checkInitialization() {
    if (!_isInitialized) {
      throw Exception(
          'LogUtils not initialized. Call LogUtils.shared.init() first.');
    }
  }

  void d(dynamic message) {
    _checkInitialization();
    _logger.d(message);
  }

  void i(dynamic message) {
    _checkInitialization();
    _logger.i(message);
  }

  void w(dynamic message) {
    _checkInitialization();
    _logger.w(message);
  }

  void e(dynamic message, [dynamic error, StackTrace? stackTrace]) {
    _checkInitialization();
    _logger.e(message, error: error, stackTrace: stackTrace);
  }

  Logger get logger {
    _checkInitialization();
    return _logger;
  }
}
